package com.lbs.service.Impl;

import com.lbs.dao.CourseDao;
import com.lbs.dao.CourseUserDao;
import com.lbs.dao.Impl.CourseDaoImpl;
import com.lbs.dao.Impl.CourseUserDaoImpl;
import com.lbs.dao.Impl.UserDaoImpl;
import com.lbs.dao.UserDao;
import com.lbs.entity.Course;
import com.lbs.entity.CourseUser;
import com.lbs.entity.User;
import com.lbs.service.CourseUserService;
import com.lbs.utils.PageUtils;

import java.util.List;

/**
 * @author Administrator
 * @date 2021/6/2 10:18
 * @description
 **/
public class CourserUserServiceImpl implements CourseUserService {

    CourseUserDao courseUserDao = new CourseUserDaoImpl();

    UserDao userDao = new UserDaoImpl();

    CourseDao courseDao = new CourseDaoImpl();

    /**
     * 分页查询选课信息
     *
     * @param search
     * @param pageSize
     * @param currentPage
     * @return
     */
    @Override
    public PageUtils<CourseUser> pageCourseUser(String search, int pageSize, int currentPage) {
        StringBuilder builder = new StringBuilder();
        String sql = "select cu.* from course_user cu,user u where cu.uid = u.uid and u.name like ?";
        builder.append(sql);
        List<CourseUser> courseUsers = courseUserDao.findCourseUserByName(builder.toString(), "%" + search + "%");
        int total = 0;
        if (courseUsers != null && courseUsers.size() > 0) {
            total = courseUsers.size();
        } else {
            total = 0;
        }
        builder.append(" limit ?,?");
        List<CourseUser> courseUserList = courseUserDao.findCourseUserByName(builder.toString(), "%" + search + "%", (currentPage - 1) * pageSize, pageSize);
        for (CourseUser courseUser : courseUserList) {
            User user = userDao.findUserById(courseUser.getUid());
            courseUser.setUser(user);
            Course course = courseDao.findCourseById(courseUser.getCid());
            courseUser.setCourse(course);
        }

        PageUtils<CourseUser> pageUtils = new PageUtils<>(pageSize, currentPage, total, courseUserList);

        return pageUtils;
    }

    /**
     * 修改选课信息
     * @param id
     * @param cid
     * @return
     */
    @Override
    public boolean updateCourseUser(int cid, int id) {

        return courseUserDao.updateCourseUserById(cid,id)>0;
    }

    /**
     * 批量删除选课信息
     * @param ids
     * @return
     */
    @Override
    public boolean batchDel(String[] ids) {
        if (ids!=null&&ids.length>0){
            for (String id : ids) {
                courseUserDao.deleteCourseById(id);
            }
            return true;
        }else {
            return false;

        }
    }
}
